package insight.google.game.era.data;

import insight.google.game.era.bo.GameRoom;
import insight.google.game.era.bo.GameUser;
import insight.google.game.era.bo.card.GameCard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;

public class GameUserDataManager extends AppDataManager {

	
	public GameUserDataManager(){
		this.isDetachable = true;
		super.initialize();
	}
	
	public GameUserDataManager(TransactionHandler txHandler){
		this.txHandler = txHandler;
		super.initialize();
	}
	
	public GameUserDataManager(TransactionHandler txHandler, boolean isDetachable){
		this.isDetachable = isDetachable;
		this.txHandler = txHandler;
		super.initialize();
	}
	
//	@Override
//	public boolean addToDataStore(Object o) {
//		try {
//			dataStoreMgr.save(o,pManager);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return false;
//	}

	@Override
	public ArrayList<GameUser> findAll() {
		List<GameUser> userList =null;
		try {
			userList = (List<GameUser>)dataStoreMgr.get(GameUser.class,pManager,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ArrayList<GameUser>(userList);
		//return null;
	}
	
	public GameUser findByName(String userName) {
		StringBuffer cond = new StringBuffer();
		cond.append("name == '" + userName + "'");
		return getSingleGameUser(cond.toString());
		
	}
	
	
	public GameUser findByGoogleId(User googUser) {
		StringBuffer cond = new StringBuffer();
		cond.append("email == '" + googUser.getEmail() + "'");
		return getSingleGameUser(cond.toString());
	}
	
	public GameUser findById(Key userId) {
		return (GameUser)dataStoreMgr.getById(GameUser.class, userId,pManager,this.isDetachable);
//		StringBuffer cond = new StringBuffer();
//		cond.append("id == '" + KeyFactory.keyToString(userId) + "'");
//		return getSingleGameUser(cond.toString());
		
	}
	
	public List<GameUser> findAllByRoomId(Key roomId) {
		
		StringBuffer cond = new StringBuffer();
		cond.append(" roomId ==:refKey");
		List<GameUser> list = null; 
		try {
			list = this.dataStoreMgr.getByConditionWithKeyParam(GameUser.class, cond.toString(), this.pManager,roomId,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
//		StringBuffer cond = new StringBuffer();
//		cond.append("roomId == '" + roomId + "'");
//		List<GameUser> list = null; 
//		try {
//			list = (List<GameUser>)DataStoreManager.getInstance().getByCondition(GameUser.class, cond.toString(),pManager,this.isDetachable);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return list;
		
	}
	
	public GameCard findCardByGenratedId(String cardGeneratedId) {
		StringBuffer cond = new StringBuffer();
		cond.append("generatedCardId == '" + cardGeneratedId + "'");
		List<GameCard> cards = null;
		try {
			cards = (List<GameCard>)DataStoreManager.getInstance().getByCondition(GameCard.class, cond.toString(),pManager,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(cards != null){
			for (GameCard gameCard : cards) {
				return gameCard;
			}
		}
		
		return null;
	}
	
	public GameCard findCardByCardId(Key cardId) {
		GameCard gameCard = null;
		List<GameCard> cardList = (List<GameCard>)DataStoreManager.getInstance().getById(GameCard.class, cardId,pManager,this.isDetachable);
		if (cardList != null) {
			for (int i = 0; i < cardList.size(); i++) {
				gameCard = (GameCard)cardList.get(i);
				break;
			}
		}
		return gameCard;
	}
	
	public List<GameCard> findAllCardsByIds(List<String> cardIdList) {
		List<GameCard> cardList = new ArrayList<GameCard>();
		GameCard card = null; 
		for (int i = 0; i < cardIdList.size(); i++) {
			card = (GameCard)dataStoreMgr.getInstance().getById(GameCard.class, KeyFactory.stringToKey(cardIdList.get(i)),pManager,this.isDetachable);
			if(card != null){
				cardList.add(card);
			}
		}
		
		return cardList;
	}
	
	private GameUser getSingleGameUser(String cond){
		GameUser gameUser = null;
		List<GameUser> userList= null;
		try {
			userList = DataStoreManager.getInstance().getByCondition(GameUser.class, cond,pManager,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (userList != null) {
			synchronized (userList) {
				for (int i = 0; i < userList.size(); i++) {
					gameUser = (GameUser)userList.get(i);
					break;
				}
			}
			
		}
		return gameUser;
	}

	

}
